import * as Pinia from 'pinia'

const pinia = createPinia()

// 使用方法1
// import { useUserStore } from '@/store/modules/user'
// let store = useUserStore()

//使用方法2
// let store = uni.$store(文件名)   默认选择第一个store  多个建议使用user/useUserStorse 具体路径和名称
// 如果有多个可以指定使用哪个 let store = uni.$store(文件名/store名称) user/useUserStorse
let list = import.meta.globEager('./modules/**.js')
let storeCollection = {}
for (const path in list) {
  let name = path.slice(10, -3)
  storeCollection[name] = list[path]
}

const store = (name) => {
  if (!storeCollection) return
  if (!store) return store(name)
  let nameKey = name.split('/')
  let item = storeCollection[nameKey[0]]
  //   如果一个文件有多个默认选中第一个store
  let storeFun = item[Object.keys(item)[0]]
  if (nameKey.length > 1) {
    try {
      storeFun = item[nameKey[1]]
    } catch {
      throw '请输入正确的store名称和路径'
    }
  }
  return storeFun()
}

export { Pinia, pinia, store }
